package lgq.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.impl.EmpDAOImpl;
import dao.impl.TrainDAOImpl;

import lgq.service.CertificateManager;
import model.Certificate;
import model.Emp;
import model.Train;

public class AddCertificateServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Emp e=new Emp();
		e=new EmpDAOImpl().getEmpById(new EmpDAOImpl().getIDByEmpno(request.getParameter("empno")));
		Certificate cf=new Certificate();
		cf.setEmp(e);
		Train t=new Train();
		t=new TrainDAOImpl().getById(Integer.parseInt(request.getParameter("trainid")));
		cf.setTrain(t);
		cf.setCername(request.getParameter("cername"));
		cf.setCerid(request.getParameter("cerno"));
		try {
			cf.setCerdate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("cerdate")));
			cf.setDeadline(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("dateline")));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		new CertificateManager().add(cf);
		request.getRequestDispatcher("/FindCertificateServlet").forward(request, response);
	}

}
